import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;


public class Chart2PDF {

	
    public static void main(String[] args) {
        try {
        	
        	FopFactory fopFactory = FopFactory.newInstance();
            System.out.println("Chart2PDF\n");
            
            // files            
            File fofile = new File("chart.fo");
            File pdffile = new File("Chart.pdf");

            
            OutputStream out = null;            

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            
            // Setup output stream.  
            out = new FileOutputStream(pdffile);
            out = new BufferedOutputStream(out);

            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup JAXP using identity transformer
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(); // identity transformer
                
            // Setup input stream
            Source src = new StreamSource(fofile);

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
                
            transformer.transform(src, res);
       
            out.close();
            System.out.println("End\n");
        } catch (Exception e) {
            e.printStackTrace();

        }
        
        System.exit(0);
        
    }
}
